package com.fxust.dialog;

/**
 * @auto johnking
 * @create 2017-12-17 下午9:16
 **/

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import com.fxust.common.Constant;
import com.fxust.thread.DownLoadPicThread;
import com.fxust.utils.QuerySqlUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * 图片下载主对话框
 *
 * @author fgq 2017年11月25日 下午1:57:00
 * @version V1.0
 */
public class DownloadPicDialog extends BaseDialog {

    /**
     * 序列化ID
     */
    private static final long serialVersionUID = 4476566145548711247L;

    /**
     * 界面控件声明
     */
    private final JPanel contentPanel = new JPanel();
    private JButton downLoadPicButton = null;
    private JButton browseButton = null;
    private JTextField filepathTextField = null;
    private JTextField startNumField = null;
    private JTextField totalNumField = null;

    // 全局唯一id
    private String uuid;

    /**
     * 创建一个新的实例DownloadPicDialog.
     *
     * @param frame
     * @param title
     * @param uuid
     */
    public DownloadPicDialog(Frame frame, String title, String uuid) {
        super(frame, title);
        this.uuid = uuid;
        setBounds(100, 100, 475, 300);
        getContentPane().setLayout(new BorderLayout());

        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPanel.setBackground(Color.WHITE);
        contentPanel.setLayout(null);
        getContentPane().add(contentPanel, BorderLayout.CENTER);

        {
            JPanel rangePanel = new JPanel();
            rangePanel.setLayout(null);
            rangePanel.setBorder(BorderFactory.createTitledBorder("数据查询范围"));
            rangePanel.setBackground(Color.WHITE);
            rangePanel.setBounds(10, 30, 438, 120);
            contentPanel.add(rangePanel);
            {

                JLabel filepathLabel = new JLabel("下载路径");
                filepathLabel.setBounds(5, 30, 65, 15);
                filepathLabel.setHorizontalAlignment(SwingConstants.RIGHT);
                rangePanel.add(filepathLabel);

                // 文件路径TextField
                filepathTextField = new JTextField();
                filepathTextField.setBounds(80, 30, 230, 21);
                filepathTextField.setEditable(false);
                filepathTextField.setColumns(10);
                rangePanel.add(filepathTextField);

                // 浏览Button
                browseButton = new JButton("浏览");
                browseButton.setBounds(325, 29, 90, 23);
                rangePanel.add(browseButton);
            }
            {
                JLabel startNumLabel = new JLabel("开始序号");
                startNumLabel.setBounds(5, 72, 65, 15);
                rangePanel.add(startNumLabel);
                startNumLabel.setHorizontalAlignment(SwingConstants.RIGHT);

                startNumField = new JTextField();
                startNumField.setColumns(10);
                startNumField.setBounds(80, 70, 80, 21);
                rangePanel.add(startNumField);

                JLabel extractNumLabel = new JLabel("查询数量");
                extractNumLabel.setHorizontalAlignment(SwingConstants.RIGHT);
                extractNumLabel.setBounds(150, 72, 70, 15);
                rangePanel.add(extractNumLabel);

                totalNumField = new JTextField();
                totalNumField.setColumns(10);
                totalNumField.setBounds(230, 70, 80, 21);
                rangePanel.add(totalNumField);

                // 开始查询Button
                downLoadPicButton = new JButton("开始下载");
                downLoadPicButton.setBounds(325, 69, 90, 23);
                rangePanel.add(downLoadPicButton);
            }

        }
        {
            JPanel processPanel = new JPanel();
            processPanel.setLayout(null);
            processPanel.setBorder(BorderFactory.createTitledBorder("下载进度"));
            processPanel.setBackground(Color.WHITE);
            processPanel.setBounds(10, 160, 438, 80);
            contentPanel.add(processPanel);
            {
                // 详情进度TextField
                dealNumTextField = new JTextField();
                dealNumTextField.setEditable(false);
                dealNumTextField.setColumns(10);
                dealNumTextField.setBounds(20, 30, 185, 21);
                processPanel.add(dealNumTextField);

                // 详情进度ProgressBar
                dealProgressBar = new JProgressBar();
                dealProgressBar.setMaximum(100);
                dealProgressBar.setBounds(230, 30, 185, 21);
                processPanel.add(dealProgressBar);
            }
        }
        addListener();
        initData();
    }

    private void addListener() {
        // [路径选择]的点击事件
        browseButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                browseFilePathClicked();
            }
        });
        // [开始下载Button]的点击事件
        downLoadPicButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                startDownLoadPicMain();
            }
        });
    }

    /**
     * 初始化sql语句到内存
     *
     * @author fgq 2017年11月25日 下午1:54:23
     */
    private void initData() {
        QuerySqlUtils.initSql();
    }

    /**
     * 开始图片下载主方法
     *
     * @author fgq 2017年11月25日 下午1:53:50
     */
    private void startDownLoadPicMain() {
        Map<String,String> paramMap = new HashMap<String,String>();
        // 验证界面值得有效性
        String filePath = filepathTextField.getText();
        if (!varifyFilePath(filePath)) {
            return;
        }
        paramMap.put(Constant.FILE_PATH, filePath);
        String startNum = startNumField.getText();
        String totalNum = totalNumField.getText();
        if(StringUtils.isNotBlank(startNum)&&StringUtils.isNotBlank(totalNum)){
            paramMap.put(Constant.START_NUM, startNum);
            paramMap.put(Constant.TOTAL_NUM, totalNum);
            paramMap.put(Constant.INPUT_FLAG, Constant.YES);
        }else{
            paramMap.put(Constant.INPUT_FLAG, Constant.NO);
        }

        // 启动线程
        new DownLoadPicThread(uuid, paramMap).start();
    }

    /**
     * 浏览文件路径
     *
     * @author fgq 2017年8月8日 下午3:26:25
     */
    private void browseFilePathClicked() {
        String filepath = openFileChooser(JFileChooser.DIRECTORIES_ONLY, null, null);
        if (StringUtils.isNotBlank(filepath)) {
            filepathTextField.setText(filepath);
            filepathTextField.setEditable(false);
        }
    }
}
